package com.sbs.gaoxinqutoilet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.entity.HomeNewsEntity;
import com.sbs.gaoxinqutoilet.utils.BannerImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 聪明一只哈 on 2019/10/28.
 * Time: 2019/10/28  15:09
 */
public class BannerAdapter extends DelegateAdapter.Adapter{

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private List<HomeNewsEntity.DataBean> mItemBeanList;
    private LayoutInflater inflater;


    public BannerAdapter(Context mContext, LayoutHelper mLayoutHelper, List<HomeNewsEntity.DataBean> mItemBeanList) {
        this.mContext = mContext;
        this.mLayoutHelper = mLayoutHelper;
        this.mItemBeanList = mItemBeanList;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new BannerViewHolder(inflater.inflate(R.layout.vlayout_banner_item,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        BannerViewHolder bannerViewHolder = (BannerViewHolder)viewHolder;
        bindBanner(bannerViewHolder);
    }

    /**
     * 轮播图样式
     */
    private void bindBanner(final BannerViewHolder holder) {
        //轮播图的常规设置
        holder.mBannerView.setIndicatorGravity(BannerConfig.RIGHT);//设置指示器局右显示
        //====加载Banner数据====
        holder.mBannerView.setImageLoader(new BannerImageLoader());//设置图片加载器
        //设置只显示指示器
        holder.mBannerView.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        holder.mBannerView.setIndicatorGravity(BannerConfig.CENTER);

        setBannerData(holder);//设置轮播图的数据
    }
    /**设置轮播图的数据*/
    private void setBannerData(final BannerViewHolder holder){

        List<String> images = new ArrayList<String>();
        final List<String> titles = new ArrayList<String>();
        for(HomeNewsEntity.DataBean itemBean : mItemBeanList){
            images.add(itemBean.getNews_logo());
            titles.add(itemBean.getNews_title());
        }

        holder.mBannerView.setImages(images);
        holder.mBannerView.isAutoPlay(true);
        //holder.mBannerView.setBannerTitles(titles);

        //banner设置方法全部调用完毕时最后调用
        holder.mBannerView.start();

        //设置viewpager的滑动监听
//        holder.mBannerView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                holder.mBannerTitle.setText(titles.get(position));
//
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//            }
//        });

        //设置点击事件，下标是从0开始
        holder.mBannerView.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if(bannerCallback != null){
                    bannerCallback.clickBanner(mItemBeanList.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {

        /*
        * 混乱现象的原因是，banner的adapter，加载数据时加载的是整个view的个数，
        * 而不是banner数据集合的个数，banner是一个view,banner只有一个，我之前
        * 写的是mItemBeanList.size()，是3个，并且helper是SingleLayoutHelper，
        * 所以多出来的两个显示到了下方
        * */

        return 1;//mItemBeanList==null?0:mItemBeanList.size()
    }

    static class BannerViewHolder extends RecyclerView.ViewHolder{

        private Banner mBannerView;
        private TextView mBannerTitle;

        public BannerViewHolder(View itemView) {
            super(itemView);
            mBannerView = (Banner) itemView.findViewById(R.id.home_banner);
            mBannerTitle = (TextView) itemView.findViewById(R.id.tv_banner_title);
        }
    }

    /**轮播图点击回调*/
    public interface BannerCallback{
        void clickBanner(HomeNewsEntity.DataBean itemBean);
    }

    private BannerCallback bannerCallback;

    public void setBannerCallback(BannerCallback bannerCallback){
        this.bannerCallback = bannerCallback;
    }
}
